//
//  DZWebViewController.m
#import "DZWebViewController.h"
#import <WebKit/WebKit.h>
@interface DZWebViewController ()
@property (nonatomic, strong) WKWebView *dzWebView;
@end
@implementation DZWebViewController
- (void)viewDidLoad {
    [self makeSubView];
    [self.dzWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    [self makeNavBack];
    self.navigationController.navigationBar.hidden = false;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = true;
}
- (void)makeNavBack{
    // 创建自定义返回按钮
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"dz_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTapped)];
    self.navigationItem.leftBarButtonItem = backBarButtonItem;
    
//    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [backButton setImage:[UIImage imageNamed:@"dz_back"] forState:UIControlStateNormal];
//    [backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
//    [backButton setFrame:CGRectMake(DZRatio(24), DZRatio(12)+DZTopSafe_H, DZRatio(64), DZRatio(64))]; // 设置按钮大小
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
//    self.navigationItem.leftBarButtonItem = leftItem;
}
- (void)backButtonTapped{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)makeSubView{
    [self.view addSubview:self.dzWebView];
    [self.dzWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(DZRatio(0)));
    }];
    if(_localUrl.length > 0){
        [self loadLocalUrl:_localUrl];
    }else if (_webUrl.length > 0){
        [self loadWebUrl:_webUrl];
    }
}
- (WKWebView *)dzWebView {
    if (!_dzWebView) {
        _dzWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:[self webviewConfiguration]];
        _dzWebView.backgroundColor = self.view.backgroundColor;
        _dzWebView.scrollView.backgroundColor = self.view.backgroundColor;
    }
    return _dzWebView;
}
- (WKWebViewConfiguration *)webviewConfiguration{
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    config.suppressesIncrementalRendering = YES; // 是否支持记忆读取
    [config.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
    [config setValue:@YES forKey:@"allowUniversalAccessFromFileURLs"];
    return config;
}
-(void)loadWebUrl:(NSString *)url{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    [self.dzWebView loadRequest:request];
}
-(void)loadLocalUrl:(NSString *)url{
    NSString *htmlFilePath = [[NSBundle mainBundle] pathForResource:url ofType:@"html"];
    // 加载本地HTML文件
    NSURL *fileURL = [NSURL fileURLWithPath:htmlFilePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
    [self.dzWebView loadRequest:request];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if([keyPath isEqualToString:@"title"]){
        self.navigationItem.title = _dzWebView.title;
    }
}
@end
